﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class ContactService : BaseService
    {
        public IList<Contact.ContactType> GetContactTypes ()
        {
            DataTable table = BaseModel.GetAllByTableName("ContactType");
            IList<Contact.ContactType> list = new List<Contact.ContactType>();

            if ( table != null )
            {
                foreach ( DataRow row in table.Rows )
                {
                    list.Add(new Contact.ContactType(BaseModel.GetIntFieldValue(row, "TypeId"))
                    {
                        Name = BaseModel.GetStringFieldValue(row, "TypeName")
                    });
                }
            }

            return list;
        }
        public IList<Contact> GetContacts(int accountId)
        {
            DataTable dtContact = (new AccountModel()).GetListContactByAccountId(accountId);
            IList<Contact> list = new List<Contact>();
            if (dtContact != null && dtContact.Rows.Count > 0)
            {
                foreach (DataRow dr in dtContact.Rows)
                {
                    Contact contact = new Contact(BaseModel.GetIntFieldValue(dr, "ContactId"))
                    {
                        FirstName = BaseModel.GetStringFieldValue(dr, "FirstName"),
                        LastName = BaseModel.GetStringFieldValue(dr, "LastName"),
                        Email = BaseModel.GetStringFieldValue(dr, "Email"),
                        Phone = BaseModel.GetStringFieldValue(dr, "Phone"),
                        Location = LocationInfo.Sample(),
                        Type = new Contact.ContactType(BaseModel.GetIntFieldValue(dr, "ContactTypeId"))
                        {
                            Name = BaseModel.GetStringFieldValue(dr, "ContactTypeName")
                        }
                    };
                    list.Add(contact);
                }
            }
            return list;
        }
        public Contact GetContact(int contactId)
        {
            using (ContactModel model = new ContactModel())
            {
                DataRow row = model.GetContact(contactId);

                if (row != null)
                {
                    return new Contact(contactId)
                    {
                        FirstName = BaseModel.GetStringFieldValue(row, "FirstName"),
                        LastName = BaseModel.GetStringFieldValue(row, "LastName"),
                        Phone = BaseModel.GetStringFieldValue(row, "Phone"),
                        Email = BaseModel.GetStringFieldValue(row, "Email"),
                        Type = new Contact.ContactType(BaseModel.GetIntFieldValue(row, "ContactTypeId")) { Name = BaseModel.GetStringFieldValue(row, "ContactTypeName") }
                    };
                }
            }

            return null;
        }
        public void SaveContact(int customerId, int loginId, Contact contact)
        {
            if (contact != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    model.InsertUpdateContact(customerId, loginId, contact.FirstName, contact.LastName, contact.Email, contact.Phone, contact.Type.Id, contact.Id);
                }
            }
        }


        public void InsertUpdateUserInfo(User user)
        {
            if (user != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    model.InsertUpdateUserInfo(user.Customer.Id, user.Id, user.Username, user.Password, user.FirstName, user.LastName, user.Email, user.Phone, 0);
                }
            }
        }

        public void SaveMainContact(Customer customer)
        {
            if (customer != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    Contact contact = customer.MainContact;
                    model.InsertUpdateAccountAddressAll(customer.Id, customer.Code, contact.FirstName, contact.LastName, customer.CompanyName, contact.Email, contact.Phone, contact.Location.Address, contact.Location.Street, "", contact.Location.City, contact.Location.State.Code, contact.Location.Country.Code, contact.Location.ZipCode);
                }
            }
        }

    }
}
